package com.skyjing.askit.java.temp;

import android.content.Intent;
import android.net.Uri;

import java.io.File;

/**
 * 用intente使用第三方软件打开文件
 */
public class FileIntentUitls {

    /**
     * get word text file intent
     * @param param {@Intent}
     * @return
     */
    public static Intent wordFileIntent(String param) {
        Uri uri = Uri.fromFile(new File(param));
        return getIntent().setDataAndType(uri, "application/msword");
    }

    /**
     * android获取一个用于打开Excel文件的intent
     * @param param
     * @return
     */
    public static Intent excelFileIntent( String param ) {
        Uri uri = Uri.fromFile(new File(param ));
        return getIntent().setDataAndType(uri, "application/vnd.ms-excel");
    }

    /**
     * android获取一个用于打开PPT文件的intent
     * @param param
     * @return
     */
    public static Intent pptFileIntent( String param ) {
        Uri uri = Uri.fromFile(new File(param ));
        return getIntent().setDataAndType(uri, "application/vnd.ms-powerpoint");
    }

    /**
     * android获取一个用于打开HTML文件的intent
     * @param param
     * @return
     */
    public static Intent htmlFileIntent(String param) {
        Uri uri = Uri.parse(param).buildUpon().
                encodedAuthority("com.android.htmlfileprovider").scheme("content")
                .encodedPath(param).build();
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.setDataAndType(uri, "text/html");
        return intent;
    }

    /**
     * android获取一个用于打开图片文件的intent
     * @param param
     * @return
     */
    public static Intent imageFileIntent(String param) {
        Uri uri = Uri.fromFile(new File(param));
        return getIntent().setDataAndType(uri, "image/*");
    }

    /**
     * android获取一个用于打开PDF文件的intent
     * @param param
     * @return
     */
    public static Intent pdfFileIntent( String param ) {
        Uri uri = Uri.fromFile(new File(param ));
        return getIntent().setDataAndType(uri, "application/pdf");
    }

    /**
     * android获取一个用于打开文本文件的intent
     * @param param
     * @param paramBoolean
     * @return
     */
    public static Intent textFileIntent( String param, boolean paramBoolean) {
        Uri uri;
        if (paramBoolean) {
            uri = Uri.parse(param );
        }
        else {
            uri = Uri.fromFile(new File(param));
        }
        return getIntent().setDataAndType(uri, "text/plain");
    }

    /**
     * android获取一个用于打开音频文件的intent
     * @param param
     * @return
     */
    public static Intent audioFileIntent( String param ) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "audio/*");
        return intent;
    }

    /**
     * android获取一个用于打开视频文件的intent
     * @param param
     * @return
     */
    public static Intent videoFileIntent( String param ) {
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(new File(param ));
        intent.setDataAndType(uri, "video/*");
        return intent;
    }

    private static Intent getIntent(){
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        return intent;
    }
}
